(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.apMemberViewController', apMemberViewController);
	apMemberViewController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig' , 'authService', 'ajaxServiceFactory', 'validatorConfig', 'apMemberService'];
	function apMemberViewController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, authService, ajaxServiceFactory, validatorConfig, apMemberService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("apMemberViewController"), ajaxService = ajaxServiceFactory.buildAjaxService({
		});
		ctrl.dialogsModule = dialogsModule;
//		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.apMember = {};
		ctrl.formName = "apMemberForm";
		
		ctrl.init = function() {
			// init acct
			apMemberService.initProfAcct(layoutConfig, $scope, ctrl);
			// init int
			//apMemberService.initProfInt(layoutConfig, $scope, ctrl);
			//apMemberService.initProfCostOfFundInt(layoutConfig, $scope, ctrl);
			// init pastdue
			apMemberService.initProfPastdue(layoutConfig, $scope, ctrl);
			// init fee
			apMemberService.initProfFee(layoutConfig, $scope, ctrl);
			// init doc
			apMemberService.initEventDoccfg(layoutConfig, $scope, ctrl);
			// init limit facility
			//apMemberService.initProfLimitFaci(layoutConfig, $scope, ctrl);
			//apMemberService.initProfLimitInsFaci(layoutConfig, $scope, ctrl);
			//apMemberService.initProfLimitPurFaci(layoutConfig, $scope, ctrl);
			// init Settlement Instruction
			apMemberService.initProfSetlinst(layoutConfig, $scope, ctrl);
			// init cust prop define
			apMemberService.initCustProp(layoutConfig, $scope, ctrl);
			//
			apMemberService.initGeneral($scope, ctrl).then(function() {
				var initApMember = ctrl.dialogsModule.params.apMember || {};
				initApMember.prodSubtypeExtProperty = ctrl.dialogsModule.params.prodSubtypeExtProperty;
				// reset data
				apMemberService.resetMemberByProdSubtypeExtProperty(ctrl, initApMember);
				ctrl.apMemberOld = ctrl.dialogsModule.params.apMemberOld;
				apMemberService.initLayoutConfigAndModel(ctrl, $scope, initApMember, "apMemberView").then(function() {
					if (ctrl.apMemberOld) {
						ctrl.apMemberOld.profAccts = ctrl.apMemberOld.profAccts || [];
						ctrl.apMemberOld.profPastdues = ctrl.apMemberOld.profPastdues || [];
						ctrl.apMemberOld.profFees = ctrl.apMemberOld.profFees || [];
						ctrl.apMemberOld.eventDoccfgs = ctrl.apMemberOld.eventDoccfgs || [];
						ctrl.apMemberOld.profSetlinsts = ctrl.apMemberOld.profSetlinsts || [];
					}
					// reset cust prop data
					ctrl.resetCustProperties();
				});
			});
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
	}
})(angular);
